package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.tea.inventory.db.dao.StorageMapper;
import org.tea.inventory.db.domain.Storage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlumniRecordStorageService {

    @Resource
    private StorageMapper storageMapper;

    /**
     * 添加文件记录
     *
     * @param record
     * @return
     */
    public int insertSelective(Storage record){
        return storageMapper.insertSelective(record);
    }

    /**
     * 根据key查找文件
     *
     * @param key
     * @return
     */
    public Storage findByKey(String key) {

        Storage example = new Storage();
        example.setKey(key);

        return storageMapper.selectOneByExample(example);
    }

    /**
     * 根据id查询文件记录
     *
     * @param id
     * @return
     */
    public Storage selectByPrimaryKey(Integer id){
        return storageMapper.selectByPrimaryKey(id);
    }
    public List<Storage> selectByPrimaryKeys(String[] id){
        return storageMapper.selectByPrimaryKeys(id);
    }
    /**
     * 根据id删除文件记录
     *
     * @param id
     * @return
     */
    public int deleteByPrimaryKey(Integer id){
        return storageMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询文件列表
     *
     * @param page
     * @param size
     * @return
     */
    public Map<String,Object> selectStorage(String key,String oldName,String startTime,String endTime,Integer page, Integer size){
        Storage example = new Storage();

        if (StringUtils.isNotEmpty(key)){
            example.setKey(key);
        }
        if (StringUtils.isNotEmpty(oldName)){
            example.setOldName(oldName);
        }

        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {

        }

        PageHelper.startPage(page, size);
        List<Storage> list = storageMapper.selectByExample(example);


        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("items",list);
        resMap.put("pageNum",pageinfo.getPageNum());
        resMap.put("pageSize", pageinfo.getPageSize());
        resMap.put("startRow", pageinfo.getStartRow());
        resMap.put("endRow", pageinfo.getEndRow());
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());

        return resMap;
    }


    /**
     * 根据id更新数据
     *
     * @param record
     * @return 更新条数
     */
    public int updateByPrimaryKeySelective(Storage record){
        return storageMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 根据url查询Storage
     * @param visitorPhoto
     * @return
     */
    public Storage findByUrl(String visitorPhoto) {
        Storage example = new Storage();
        example.setUrl(visitorPhoto);

        return storageMapper.selectOneByExample(example);
    }
}
